<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected $_config;

    public function _initAutoregister()
    {
        require_once 'Zend/Loader/Autoloader.php';
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->setFallbackAutoloader(true);
    }

    function _initConfig()
    {
        $this->_config = new Zend_Config_Ini(APPLICATION_PATH . "/configs/application.ini",APPLICATION_ENV);
        Zend_Registry::set('config',$this->_config);
        Zend_Registry::set('env',APPLICATION_ENV);
    }
    
    protected function _initAutoload()
    {
        $front = Zend_Controller_Front::getInstance();
         
        $front->addModuleDirectory( APPLICATION_PATH . "/modules");
        $front->setDefaultModule( "default" );
         
         
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath' => dirname(__FILE__),
        ));

        $frontController = Zend_Controller_Front::getInstance();
        $frontController->registerPlugin(new Default_Controllers_Plugin_Loader());

        return $autoloader;
    }

    protected function _initConltrols()
    {
        require_once 'Crb/Controller.php';
        require_once 'Crb/Settings.php';

        Zend_Layout::startMvc(array('layoutPath' => APPLICATION_PATH . '/views/layouts'));
        $layout = Zend_Layout::getMvcInstance();
        $layout->setLayoutPath(APPLICATION_PATH . "/views/layouts");

        $frontController = Zend_Controller_Front::getInstance();
        if ($this->_config->baseUrl)
        {
            $frontController->setBaseUrl($this->_config->baseurl);
        }
    }


    protected function _initDB()
    {
        if ($this->_config->resources->db)
        {
            $dbAdapter = Zend_Db::factory($this->_config->resources->db);
            $dbAdapter->query("SET NAMES 'utf8'");

            
            
            if ($this->_config->resources->db->profilerFirebug)
            {
                $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
                $profiler->setEnabled(true);
                $dbAdapter->setProfiler($profiler);
            }


            Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
            
            Zend_Registry::set('dbAdapter',$dbAdapter);
            
            return $dbAdapter;
        }
    }

    public function _initRouters()
    {
        $ctrl = Zend_Controller_Front::getInstance();
        $router = $ctrl->getRouter();
         

        $router->addRoute('default', new Zend_Controller_Router_Route(
          ':module/:controller/:action/*',
            array(
            'module'    => 'default',
            'controller'  => 'index',
            'action'    => 'index'
        )
        ));

        $router->addConfig($this->_config,'route');
        $compat = new Zend_Controller_Router_Route_Module(array(),
        $ctrl->getDispatcher(),
        $ctrl->getRequest());

         
        $router->addRoute('default', $compat);
    }

    public function _initRoles()
    {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()){
            $User = $auth->getIdentity();

            return $User->role;
        }
        else{
            return 0;
        }
    }

    public function _initAcl()
    {
        $aclConfig = $this->_config->ACL;
        if (!$aclConfig->enabled) return;

        $acl = new Acl();
        $ctrl = Zend_Controller_Front::getInstance();
        $ctrl->registerPlugin(new Aclplugin($acl));
        return $acl;
    }

    public function _initPerfomance()
    {
        if ($this->_config->performance->cachingEnabled){
            require_once 'Zend/Loader/PluginLoader.php';
            $classFileIncCache = APPLICATION_PATH . '/../Cache/PluginLoader/Cache.php';
            if (file_exists($classFileIncCache)) include_once $classFileIncCache;
            Zend_Loader_PluginLoader::setIncludeFileCache($classFileIncCache);

            $frontendOptions = array( 'automatic_serialization' => true);
            $backendOptions  = array( 'cache_dir' => APPLICATION_ROOT . DIRECTORY_SEPARATOR . "Cache/Global");
            $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
             
            Zend_Registry::set('cacheGlobal', $cache);

            $frontendOptions = array( 'automatic_serialization' => true);
            $backendOptions  = array( 'cache_dir' => APPLICATION_ROOT . DIRECTORY_SEPARATOR . "Cache/Meta");
            $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
            Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
        }
    }


    public function _initSEO()
    {
        $headTitle = new Zend_View_Helper_HeadTitle();
        $headMeta = new Zend_View_Helper_HeadMeta();

        $headTitle->headTitle($this->_config->seo->title );
        $headMeta->headMeta($this->_config->seo->keywords . " ",'keywords');
        $headMeta->headMeta($this->_config->seo->description . " ",'description');
    }
}

